package multi_thread.train;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Cheng Jun
 * Description: 3个线程 交替打印 字母A 字母B 字母C
 * @version 1.0
 * @date 2022/6/9 15:53
 */
public class AlternatePrint2 {

    static ReentrantLock lock = new ReentrantLock();
    static Condition conditionA = lock.newCondition();
    static Condition conditionB = lock.newCondition();
    static Condition conditionC = lock.newCondition();
    static volatile int flagNum = 1;

    public static void main(String[] args) {
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
        PrintA printA = new PrintA();
        PrintB printB = new PrintB();
        PrintC printC = new PrintC();
        fixedThreadPool.execute(printA);
        fixedThreadPool.execute(printB);
        fixedThreadPool.execute(printC);
    }

    static class PrintA implements Runnable {
        @Override
        public void run() {
            lock.lock();
            try {
                while (true) {
                    if (flagNum == 1) {
                        System.out.println(Thread.currentThread().getName() + " A");
                        flagNum = 2;
                        conditionB.signal();
                    } else {
                        try {
                            conditionA.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            } finally {
                lock.unlock();
            }
        }
    }

    static class PrintB implements Runnable {
        @Override
        public void run() {
            lock.lock();
            try {
                while (true) {
                    if (flagNum == 2) {
                        System.out.println(Thread.currentThread().getName() + " B");
                        flagNum = 3;
                        conditionC.signal();
                    } else {
                        try {
                            conditionB.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            } finally {
                lock.unlock();
            }
        }
    }

    static class PrintC implements Runnable {
        @Override
        public void run() {
            lock.lock();
            try {
                while (true) {
                    if (flagNum == 3) {
                        System.out.println(Thread.currentThread().getName() + " C");
                        flagNum = 1;
                        conditionA.signal();
                    } else {
                        try {
                            conditionC.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }

            } finally {
                lock.unlock();
            }
        }
    }

}
